<template>
  <div>
    <Select @selectOptions="selectOptions" />
    <el-table v-loading="listLoading"
              :data="list"
              element-loading-text="数据加载中"
              border
              stripe
              fit
              highlight-current-row>
      <el-table-column label="序号"
                       width="70"
                       align="center"
                       type="index"
                       :index="indexMethod" />
      <el-table-column prop="name"
                       label="名称"
                       width="80" />
      <el-table-column prop="level"
                       label="头衔"
                       width="80"
                       :formatter="formatter" />
      <el-table-column prop="intro"
                       label="资历" />
      <el-table-column prop="gmtCreate"
                       label="添加时间"
                       width="160" />
      <el-table-column prop="sort"
                       label="排序"
                       width="60" />
      <el-table-column prop="sort"
                       label="操作"
                       width="200"
                       align="center"> <template slot-scope="scope">
          <router-link :to="'/edu/teacher/edit/'+scope.row.id">
            <el-button type="primary"
                       size="mini"
                       icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger"
                     size="mini"
                     icon="el-icon-delete"
                     @click="removeDataById(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page.sync="page"
                   :page-sizes="[10, 20, 30, 40]"
                   :page-size="limit"
                   layout="sizes, prev, pager, next"
                   style="padding: 30px 0; text-align: center;"
                   :total="total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange" />
  </div>
</template>

<script>
import teacher from '@/api/edu/teacher'

import Select from './listitem/Select'
export default {
  components: {
    Select
  },
  data() {
    return {
      listLoading: true,
      list: null,
      total: 0,
      page: 1,
      limit: 10, // 每页记录数
      searchObj: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.limit = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.fetchData(val)
    },
    fetchData(page = 1) {
      this.page = page
      this.listLoading = false
      teacher.getPageList(this.page, this.limit, this.searchObj).then(res => {
        if (res.success === true) {
          console.log(this.list)
          this.list = res.data.rows
          this.total = res.data.total
        }
        this.listLoading = false
      })
    },
    indexMethod(index) {
      return index + 1
    },
    formatter(row, column) {
      return row.level === 1 ? '高级讲师' : '首席讲师'
    },
    // 根据id删除用户
    removeDataById(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return teacher.removeById(row.id)
      }).then(() => {
        this.fetchData()
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch(res => {
        if (res === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        } else {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      })
    },
    // 获取查询条件
    selectOptions(data) {
      this.searchObj = data
      // console.log(this.searchObj)
      this.fetchData()
    }
  }

}

</script>

<style>
</style>
